import { Request, Response } from 'express';
import { homeModuleService } from './service';
import { HomeModuleType } from '../../entities/HomeModule';

/**
 * 获取首页可用模块列表 (APP接口)
 * 返回所有激活状态的模块，按排序号排序
 */
export const getActiveModules = async (req: Request, res: Response) => {
  try {
    // 只获取激活的模块
    const { modules } = await homeModuleService.getHomeModules({
      isActive: true
    });
    
    // 简化返回数据，只包含前端需要的字段
    const simplifiedModules = modules.map(module => ({
      id: module.id,
      type: module.type,
      moduleName: module.moduleName,
      title: module.title,
      imageUrl: module.imageUrl,
      videoUrl: module.videoUrl,
      content: module.content,
      description: module.description,
      sortOrder: module.sortOrder
    }));
    
    res.json({
      success: true,
      data: simplifiedModules
    });
  } catch (error: any) {
    res.json({
      success: false,
      message: error.message || '获取首页模块列表失败'
    });
  }
};